package com.ruoyi.web.controller.yf;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yf.domain.YfTruck;
import com.ruoyi.yf.service.IYfTruckService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 医废车Controller
 * 
 * @author XiaoYi...
 * @date 2021-06-24
 */
@Controller
@RequestMapping("/yf/truck")
public class YfTruckController extends BaseController
{
    private String prefix = "yf/truck";

    @Autowired
    private IYfTruckService yfTruckService;

    @RequiresPermissions("yf:truck:view")
    @GetMapping()
    public String truck()
    {
        return prefix + "/truck";
    }

    /**
     * 查询医废车列表
     */
    @RequiresPermissions("yf:truck:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YfTruck yfTruck)
    {
        startPage();
        List<YfTruck> list = yfTruckService.selectYfTruckList(yfTruck);
        return getDataTable(list);
    }

    /**
     * 导出医废车列表
     */
    @RequiresPermissions("yf:truck:export")
    @Log(title = "医废车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YfTruck yfTruck)
    {
        List<YfTruck> list = yfTruckService.selectYfTruckList(yfTruck);
        ExcelUtil<YfTruck> util = new ExcelUtil<YfTruck>(YfTruck.class);
        return util.exportExcel(list, "truck");
    }

    /**
     * 新增医废车
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存医废车
     */
    @RequiresPermissions("yf:truck:add")
    @Log(title = "医废车", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YfTruck yfTruck)
    {
        return toAjax(yfTruckService.insertYfTruck(yfTruck));
    }

    /**
     * 修改医废车
     */
    @GetMapping("/edit/{truckId}")
    public String edit(@PathVariable("truckId") Long truckId, ModelMap mmap)
    {
        YfTruck yfTruck = yfTruckService.selectYfTruckById(truckId);
        mmap.put("yfTruck", yfTruck);
        return prefix + "/edit";
    }

    /**
     * 修改保存医废车
     */
    @RequiresPermissions("yf:truck:edit")
    @Log(title = "医废车", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YfTruck yfTruck)
    {
        return toAjax(yfTruckService.updateYfTruck(yfTruck));
    }

    /**
     * 删除医废车
     */
    @RequiresPermissions("yf:truck:remove")
    @Log(title = "医废车", businessType = BusinessType.DELETE)
    @RequestMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        String s = yfTruckService.selectTagCount(ids);
        if ( Integer.parseInt(s)> 0) {
            return AjaxResult.warn("车辆参与过医废运输,不允许删除");
        }
        return toAjax(yfTruckService.deleteYfTruckByIds(ids));
    }
}
